<template lang="html">
  <div>
    <!-- <div class="search">
        <div class="input">
            <p><label>模板名称：</label><el-input placeholder="请输入内容"></el-input></p>
            <p><el-button type="primary" class="se" style="margin-left:10px;" @click="search">搜索</el-button></p>
        </div>
    </div> -->
    <div class="add">
       <el-button type="primary" @click="add">新增</el-button>
    </div>
    <tableVue :list="list" :state="false" v-loading="pageData.loading" :total="total" @changeData="selectionVal" @sizeChange="handSize">
        <slot>
          <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  @click="handleEdit(scope.row,1)">编辑
                </el-button>
                <el-button
                  size="mini"
                  type="danger"
                  class="del"
                  @click="handleEdit(scope.row,2)">删除</el-button>
              </template>
          </el-table-column>
        </slot>
    </tableVue>
    <dialogVue title="操作" :dialog="dialog" @close="changeData" :state="false">
        <slot>
            <el-form :model="pageData" ref="ruleForm" label-width="200px" class="demo-ruleForm">
              <el-form-item label="智能检测选项编码">
                  <el-input v-model="pageData.optionCode" placeholder="请输入内容"></el-input>
               </el-form-item>
               <el-form-item label="智能检测选项名称">
                   <el-input v-model="pageData.optionName" placeholder="请输入内容"></el-input>
                </el-form-item>
                <el-form-item label="是否允许别名">
                  <el-select v-model="pageData.isAlias" placeholder="请选择">
                      <el-option value="">请选择</el-option>
                      <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                      </el-option>
                    </el-select>
                 </el-form-item>
                 <el-form-item class="confirm">
                    <el-button type="primary" v-if="btnState == '1'" @click="submitForm(null,'post')">提交</el-button>
                    <el-button type="primary" v-if="btnState == '2'" @click="upForm('')">更新</el-button>
                 </el-form-item>
            </el-form>
        </slot>
    </dialogVue>
  </div>
</template>

<script>
  import Data from './localData.js'
  import dialogVue from '@/components/dialog.vue'
  import tableVue from '@/components/alltable.vue'
  import checkPermission from "@/utils/permission"; // 权限判断函数
  import {
    localList,
    addLocal,
    delLocal
  } from "@/api/product/product";
  export default {
    components : {
      tableVue,
      dialogVue
    },
    data () {
      return JSON.parse(JSON.stringify(Data))
    },
    mounted () {
        this.getLocalList();
    },
    methods : {
      getLocalList() {
        localList().then(res=>{
          console.log(res);
            this.pageData.loading = true;
          if (res.data.code == 'OK') {

            this.total = res.data.data.total;
            this.list.tbody = res.data.data.map(item=>{
               item.gmtCreate = this.switchTime(item.gmtCreate)
               item.gmtUpdate = this.switchTime(item.gmtUpdate)
               item.isAlias = item.isAlias == 'Y' ? '是' : '否'
               return item;
            });
            this.pageData.loading = false;
          } else {
            this.$message.error(res.data.message);
          }
        })
      },
      submitForm (cpidtTplId,method) {
          let data = {
            optionCode : this.pageData.optionCode,
            optionName : this.pageData.optionName,
            isAlias : this.pageData.isAlias,
            cpidtTplId : this.pageData.cpidtTplId == undefined ? null : this.pageData.cpidtTplId
          }
          addLocal(data,method).then(res=>{
            if (res.data.code == 'OK') {
                if (method == 'post') {
                    this.$message.success('新增成功');
                } else {
                    this.$message.success('编辑成功');
                }
                  this.dialog = false;
                  this.getLocalList();
            } else {
              this.$message.error(res.data.message);
            }
          })
      },
      upForm () {
        this.submitForm(this.pageData.cpidtTplId,'put')
      },
      add () {
        this.dialog = true
        this.btnState = '1'
      },
      handSize () {

      },
      search () {

      },
      handleEdit (item,num) {
        if (num ==  1) {
          this.dialog = true;
          this.btnState = '2'
          this.pageData.optionCode = item.optionCode
          this.pageData.optionName = item.optionName
          this.pageData.isAlias = item.isAlias
          this.pageData.cpidtTplId = item.cpidtTplId
        } else {
          this.delMessage(()=>{
            delLocal(item.cpidtTplId).then(res=>{
              if (res.data.code == 'OK') {
                  this.$message.success('删除成功');
                  this.getLocalList();
              } else {
                this.$message.error(res.data.message);
              }
            })
          })
        }
      },
      changeData (val) {
        this.dialog = val;
      },
      selectionVal () {

      }
    }
  }
</script>

<style scoped>
.search {
  width: 100%;
  background-color: #fff;
  padding:20px;
  box-sizing: border-box;
  margin-top: 10px;
}
.input {
  /* width: 300px; */
  display: flex;
  flex-wrap: wrap;
}
.input p {
  margin-top: 10px;
}
label {
  line-height: 40px;
  color: #333;
  margin: 0 10px;
}
.el-input {
  width:200px;
  height:20px;
}
.add {
  margin-top: 10px;
  padding-left: 14px;
}
.se {
  margin-left: 20px;
}
.confirm {
  position: absolute;
  bottom: -1px;
  right: 100px;
}
</style>
